﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using Utilities;
using MySql.Data.MySqlClient;

namespace Maticsoft.DAL
{
    //conpany
    public partial class conpany
    {

        public bool Exists(string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from conpany");
            strSql.Append(" where ");
            strSql.Append(" ConpanyName = '" + name + "'  ");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@name",MySqlDbType.VarChar,255)
            };
            parameters[0].Value = name;

            return DbHelperMySQL.Exists(strSql.ToString());

        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Maticsoft.Model.conpany model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into conpany(");
            strSql.Append("ConpanyName,Contacts,Phone,Address,Source");
            strSql.Append(") values (");
            strSql.Append("@ConpanyName,@Contacts,@Phone,@Address,@Source");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            MySqlParameter[] parameters = {
                        new MySqlParameter("@ConpanyName", MySqlDbType.VarChar,255) ,
                        new MySqlParameter("@Contacts", MySqlDbType.VarChar,255) ,
                        new MySqlParameter("@Phone", MySqlDbType.VarChar,255) ,
                        new MySqlParameter("@Address", MySqlDbType.VarChar,255),
                        new MySqlParameter("@Source", MySqlDbType.VarChar,255)

            };

            parameters[0].Value = model.ConpanyName;
            parameters[1].Value = model.Contacts;
            parameters[2].Value = model.Phone;
            parameters[3].Value = model.Address;
            parameters[4].Value = model.Source;

            object obj = DbHelperMySQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Maticsoft.Model.conpany model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update conpany set ");

            strSql.Append(" ConpanyName = @ConpanyName , ");
            strSql.Append(" Contacts = @Contacts , ");
            strSql.Append(" Phone = @Phone , ");
            strSql.Append(" Address = @Address  ");
            strSql.Append(" where Id=@Id ");

            MySqlParameter[] parameters = {
                        new MySqlParameter("@Id", MySqlDbType.Int32,11) ,
                        new MySqlParameter("@ConpanyName", MySqlDbType.VarChar,255) ,
                        new MySqlParameter("@Contacts", MySqlDbType.VarChar,255) ,
                        new MySqlParameter("@Phone", MySqlDbType.VarChar,255) ,
                        new MySqlParameter("@Address", MySqlDbType.VarChar,255)

            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.ConpanyName;
            parameters[2].Value = model.Contacts;
            parameters[3].Value = model.Phone;
            parameters[4].Value = model.Address;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from conpany ");
            strSql.Append(" where Id=@Id");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@Id", MySqlDbType.Int32)
            };
            parameters[0].Value = Id;


            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from conpany ");
            strSql.Append(" where ID in (" + Idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.conpany GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id, ConpanyName, Contacts, Phone, Address  ");
            strSql.Append("  from conpany ");
            strSql.Append(" where Id=@Id");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@Id", MySqlDbType.Int32)
            };
            parameters[0].Value = Id;


            Maticsoft.Model.conpany model = new Maticsoft.Model.conpany();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.ConpanyName = ds.Tables[0].Rows[0]["ConpanyName"].ToString();
                model.Contacts = ds.Tables[0].Rows[0]["Contacts"].ToString();
                model.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                model.Address = ds.Tables[0].Rows[0]["Address"].ToString();

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM conpany ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM conpany ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperMySQL.Query(strSql.ToString());
        }


    }
}

